package cn.icanci.ladybird.service.util.impl;

import cn.icanci.ladybird.service.util.JavaMailByQqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author: icanci
 * @date: Created in 2020/10/3 21:24
 * @classAction: 邮件发送的Service类
 */
@Service
public class JavaMailByQqServiceImpl implements JavaMailByQqService {
    @Autowired
    JavaMailSenderImpl mailSender;

    /**
     * 测试邮件发送
     *
     * @param message
     * @param targetEmailAddress
     */
    @Override
    public void sendMail(String message, String targetEmailAddress) {
        // 一个简单的邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 标题
        simpleMailMessage.setSubject("通知：少年你好");
        // 内容
        simpleMailMessage.setText(message);
        // 发给谁
        simpleMailMessage.setTo(targetEmailAddress);
        // 谁发的
        simpleMailMessage.setFrom("1845666903@qq.com");
        // 发送
        mailSender.send(simpleMailMessage);
    }

    @Override
    public void sendMail(String subject, String text, String to, String from) {
        // 一个简单的邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 标题
        simpleMailMessage.setSubject(subject);
        // 内容
        simpleMailMessage.setText(text);
        // 发给谁
        simpleMailMessage.setTo(to);
        // 谁发的
        simpleMailMessage.setFrom(from);
        // 发送
        mailSender.send(simpleMailMessage);
    }

    @Override
    public void sendMoreMail(String subject, String text, String from, String to) throws javax.mail.MessagingException {
        // 一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        // 标题
        helper.setSubject(subject);
        // 发送的内容
        // 设置内容，是否为html数据
        helper.setText(text, true);
        // 谁发的
        helper.setFrom(from);
        // 发给谁
        helper.setTo(to);
        mailSender.send(mimeMessage);
    }

    @Override
    public void sendMoreMail(String subject, String text, String from, String to, Map<String, File> map) throws javax.mail.MessagingException {
        // 一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        // 标题
        helper.setSubject(subject);
        // 发送的内容
        // 设置内容，是否为html数据
        helper.setText(text, true);
        // 附件
        if (null != map && !map.isEmpty()) {
            Set<Map.Entry<String, File>> entries = map.entrySet();
            Iterator<Map.Entry<String, File>> iterator = entries.iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, File> next = iterator.next();
                helper.addAttachment(next.getKey(), next.getValue());
            }
        }
        // 谁发的
        helper.setFrom(from);
        // 发给谁
        helper.setTo(to);
        mailSender.send(mimeMessage);
    }
}
